﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Fotobanka
{
    public partial class PridatSkupinu : System.Web.UI.Page
    {
        GroupManager gm;
        LoginInfo li;
        protected void Page_Load(object sender, EventArgs e)
        {
            gm = new GroupManager();
            li = (LoginInfo)Session["logged_user_id"];

            if (ObjectDataSource1.SelectParameters["user_id"] == null)
            {
                ObjectDataSource1.SelectParameters.Add(new Parameter("user_id", TypeCode.Int32, li.Id.ToString()));
            }

            switch ((int)li.Role)
            {
                case (int)Utils.SystemRoles.Admin:
                    break;
                case (int)Utils.SystemRoles.Spravce:
                    spravce.Enabled = false;
                    break;
                case (int)Utils.SystemRoles.Uzivatel:
                    break;
                default:

                    break;
            }
        }

        protected void Page_PreRenderComplete(object sender, EventArgs e)
        {

            spravce.Items.FindByValue((li != null) ? li.Id.ToString() : "0").Selected = true;
        }

        protected void addButton_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                gm.InsertToDb(jmeno.Text, popis.Text, Convert.ToInt32(spravce.SelectedItem.Value));
                Response.Redirect("ListSkupin.aspx");
            }
        }

        protected void jmenoDupliValidator_ServerValidate(object source, ServerValidateEventArgs args)
        {
            if (gm.GroupAlreadyExist(jmeno.Text, Convert.ToInt32(spravce.SelectedItem.Value)))
                args.IsValid = false;
            else
                args.IsValid = true;
        }
    }
}
